package com.springboot.cloud.common.core.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */
public class EncryptUtils {
    /**
     * ARCFOUR 加密、解密
     *
     * @param data 加密数据
     */
    public static String setEncrypt(String data, String secret) {
        // 随机生成密钥
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.ARCFOUR.getValue(), secret.getBytes()).getEncoded();
        // 创建 ARCFOUR 对象
        SymmetricCrypto arcfour = new SymmetricCrypto(SymmetricAlgorithm.ARCFOUR, key);
        // 加密
        String encrypt = arcfour.encryptHex(data);
        return encrypt;
    }

    public static String setEncrypt(String data) {
        String secret = "zswedxcfrtgvbhybhujng";
        // 随机生成密钥
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.ARCFOUR.getValue(), secret.getBytes()).getEncoded();
        // 创建 ARCFOUR 对象
        SymmetricCrypto arcfour = new SymmetricCrypto(SymmetricAlgorithm.ARCFOUR, key);
        // 加密
        String encrypt = arcfour.encryptHex(data);
        return encrypt;
    }

    /**
     * ARCFOUR 加密、解密
     *
     * @param encrypt 加密数据
     */
    public static String getEncrypt(String encrypt) {
        String secret = "zswedxcfrtgvbhybhujng";
        // 随机生成密钥
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.ARCFOUR.getValue(), secret.getBytes()).getEncoded();
        // 创建 ARCFOUR 对象
        SymmetricCrypto arcfour = new SymmetricCrypto(SymmetricAlgorithm.ARCFOUR, key);
        // 解密
        String decrypt = arcfour.decryptStr(encrypt);
        return decrypt;
    }

    public static String getEncrypt(String encrypt, String secret) {
        // 随机生成密钥
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.ARCFOUR.getValue(), secret.getBytes()).getEncoded();
        // 创建 ARCFOUR 对象
        SymmetricCrypto arcfour = new SymmetricCrypto(SymmetricAlgorithm.ARCFOUR, key);
        // 解密
        String decrypt = arcfour.decryptStr(encrypt);
        return decrypt;
    }
}
